package com.yunfangdata.web.controller;

import com.alibaba.druid.support.json.JSONUtils;
import com.yunfangdata.query.ChartQueryObject;
import com.yunfangdata.service.IChartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
@RequestMapping("chart")
public class ChartController {
    @Autowired
    private IChartService service;


    @RequestMapping({"view"})
    public String client(Model model) {
        model.addAttribute("js");
        return "chart/view";
    }

    @RequestMapping({"queryClient"})
    @ResponseBody
    public Object queryClient(ChartQueryObject qo, Map map) {
        List<Map<String, String>> list = this.service.queryClientChart(qo);
        return list;
    }
 
    @RequestMapping({"queryGroupType"})
    @ResponseBody
    public List<?> queryGroupType() {
        return ChartQueryObject.grouptypes;
    }

    @RequestMapping({"queryFormatClient"})
    @ResponseBody
    public Object queryFormatClient(ChartQueryObject qo) {
        List<Map<String, String>> list = this.service.queryClientChart(qo);
        return list;
    }

    @RequestMapping({"clientByPie"})
    public String clientByPie(ChartQueryObject qo, Model model) {
        List<Map<String, String>> list = this.service.queryClientChart(qo);
        List<String> legend = new ArrayList();
        List<Map<String, String>> datas = new ArrayList();
        Iterator var6 = list.iterator();

        while(var6.hasNext()) {
            Map<String, String> map = (Map)var6.next();
            legend.add(map.get("groupType"));
            Map<String, String> data = new HashMap();
            data.put("name", map.get("groupType"));
            data.put("value", map.get("totalPay"));
            datas.add(data);
        }

        model.addAttribute("title", "'正式学员报表'");
        model.addAttribute("groupType", (Object)null);
        model.addAttribute("legend", JSONUtils.toJSONString(legend));
        model.addAttribute("datas", JSONUtils.toJSONString(datas));
        return "chart/pie";
    }

    @RequestMapping({"clientByBar"})
    public String clientByBar(ChartQueryObject qo, Model model) {
        List<Map<String, String>> list = this.service.queryClientChart(qo);
        List<String> xDatas = new ArrayList();
        List<String> yDatas = new ArrayList();
        Iterator var6 = list.iterator();

        while(var6.hasNext()) {
            Map<String, String> map = (Map)var6.next();
            xDatas.add(map.get("groupType"));
            yDatas.add(map.get("totalFee"));
        }

        model.addAttribute("title", "'正式学员报表'");
        model.addAttribute("series", "'未付总款'");
        model.addAttribute("totalFee", "已付总款");
        model.addAttribute("groupType", (Object)null);
        model.addAttribute("x", JSONUtils.toJSONString(xDatas));
        model.addAttribute("y", JSONUtils.toJSONString(yDatas));
        return "chart/bar";
    }

    @RequestMapping({"formalClientByPie"})
    public String formalClientByPie(ChartQueryObject qo, Model model) {
        List<Map<String, String>> list = this.service.queryFormatClientChart(qo);
        List<String> legend = new ArrayList();
        List<Map<String, String>> datas = new ArrayList();
        Iterator var6 = list.iterator();

        while(var6.hasNext()) {
            Map<String, String> map = (Map)var6.next();
            legend.add(map.get("groupType"));
            Map<String, String> data = new HashMap();
            data.put("name", map.get("groupType"));
            data.put("totalFee", map.get("totalFee"));
            data.put("totalPay", map.get("totalPay"));
            datas.add(data);
        }

        model.addAttribute("title", "'正式学员报表'");
        model.addAttribute("groupType", (Object)null);
        model.addAttribute("legend", JSONUtils.toJSONString(legend));
        model.addAttribute("datas", JSONUtils.toJSONString(datas));
        System.out.println(datas);
        return "chart/pie";
    }

    @RequestMapping({"formalClientByBar"})
    public String formalClientByBar(ChartQueryObject qo, Model model) {
        System.out.println(11);
        List<Map<String, String>> list = this.service.queryFormatClientChart(qo);
        List<String> xDatas = new ArrayList();
        System.out.println(xDatas);
        List<String> yDatas = new ArrayList();
        Iterator var6 = list.iterator();

        while(var6.hasNext()) {
            Map<String, String> map = (Map)var6.next();
            xDatas.add(map.get("groupType"));
            yDatas.add(map.get("totalFee"));
            yDatas.add(map.get("totalPay"));
        }

        model.addAttribute("title", "'正式学员报表'");
        model.addAttribute("series", "'正式学员报表'");
        model.addAttribute("groupType", (Object)null);
        model.addAttribute("x", JSONUtils.toJSONString(xDatas));
        model.addAttribute("y", JSONUtils.toJSONString(yDatas));
        return "chart/bar";
    }
}
